package com.settlement.system.controller;

import com.settlement.system.model.entity.common.*;
import com.settlement.system.reply.ObjectDataResponse;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblWpAuditCloseAccountService;
import com.alibaba.excel.context.AnalysisContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;

import com.settlement.system.service.ITblWpFinAdjustAccountService;
import org.springframework.web.multipart.MultipartFile;

/**
 * WP-财务调账表 前端控制器
 * @author xiaochuan
 * @since 2020-11-19
 */

@Api(tags = "WP-财务调账表",description = "WP-财务调账表")
@RestController
@RequestMapping("/wp/fin/adjust/account")
@Slf4j
public class TblWpFinAdjustAccountController extends ExcelController<TblWpFinAdjustAccount> {

    @Resource
    private ITblWpFinAdjustAccountService tblWpFinAdjustAccountService;

    @Resource
    private ITblWpAuditCloseAccountService tblWpAuditCloseAccountService;

    @Override
    public Class<?> getModelClass() {
        return TblWpFinAdjustAccount.class;
    }

    @Override
    public IBaseService init() {
        return tblWpFinAdjustAccountService;
    }


    @Override
    @PostMapping("import")
    @CacheEvict(cacheNames = {"wpAccountByShopNo"},allEntries = true,beforeInvocation = true)
    public ObjectDataResponse importFile(MultipartFile file, HttpServletRequest request) {
        return super.importFile(file, request);
    }


    @Override
    public String verifyImportObject(TblWpFinAdjustAccount tblWpFinAdjustAccount, AnalysisContext analysisContext) {

        if(tblWpFinAdjustAccount.getOutDate() == null){
            return "销售日期不能为空";
        }

        if(tblWpFinAdjustAccount.getAdjustAmount() == null){
            return "调整金额不能为空";
        }

        if (StringUtils.isEmpty(tblWpFinAdjustAccount.getShopNo())) {
            return "零售店铺编码不能为空";
        }

        if (StringUtils.isEmpty(tblWpFinAdjustAccount.getBarCode())) {
            return "条码不能为空";
        }

        try {

            if (!StringUtils.isEmpty(tblWpFinAdjustAccount.getShopNo())) {
                TblWpAuditCloseAccount closeAccount = tblWpAuditCloseAccountService.getByShopNo(tblWpFinAdjustAccount.getShopNo());
                if (closeAccount == null) {
                    return "店铺在WP稽核锁帐设置中不存在";
                }
                tblWpFinAdjustAccount.setShopName(closeAccount.getShopName());

                if (closeAccount == null) {
                    return "请先设置店铺"+tblWpFinAdjustAccount.getShopNo()+"的WP稽核关账";
                }

                if (tblWpFinAdjustAccount.getOutDate().compareTo(closeAccount.getRealCloseAccountDate()) <= 0) {
                    return "销售日期必须大于关账日";
                }
            }

        } catch (Exception ex) {
            log.error("验证WP稽核调账导入异常", ex);
            return "系统异常";
        }
        return null;
    }
}

